package com.example.wms;

import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
import com.example.classes.DataBase;
import com.example.classes.Item;
import com.example.classes.Member;

/**
 * Activity for managing the Item screen.
 * @author      Alexander Huynh <a.huynh @ gatech.edu>
 * @version     1.0
 * @since       2013-03-11
 */
public class ItemHandler extends Activity
{
    private Member owner;
    private Item item;
    private boolean updated;
    private TextView iStatus;

    @Override public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_screen);

        updated = false;      //true if this item is modified

        Intent intent = getIntent();
        owner = (Member)intent.getSerializableExtra("username");
        item = (Item)intent.getSerializableExtra("itemname");
        boolean sameuser = getIntent().getBooleanExtra("sameuser", false);


        Log.v("WMS-DEBUG","ITEMHANDLER: Item="+item.getName()+"\n Item Owner= "+owner.getName()+"\n Owner Contact: "+owner.getContact());


        TextView iName = (TextView) findViewById(R.id.item_name);
        iName.setText(item.toString());

        TextView iDesc = (TextView) findViewById(R.id.item_desc);
        iDesc.setText(item.getDescription());

        iStatus = (TextView)findViewById(R.id.item_status);
        iStatus.setText((item.isResolve()?"Closed":"Open"));

        TextView iDate = (TextView) findViewById(R.id.item_date);
        iDate.setText(item.getDate().toString());

        TextView iLoc = (TextView) findViewById(R.id.item_location);
        Location loc = item.getLocation();
        if (loc != null && loc.getLongitude() != 0 && loc.getLatitude() != 0)
            iLoc.setText(("Location: " + loc.getLatitude() + "," + loc.getLongitude()));
        else
            iLoc.setText(("No Location given"));

        TextView iCat = (TextView) findViewById(R.id.item_cat);
        iCat.setText("Category: " + item.getCategory());

        TextView iRew = (TextView) findViewById(R.id.item_reward);
        iRew.setText("Reward: $" + item.getReward());

        TextView iReq = (TextView) findViewById(R.id.item_requ);
        iReq.setText("Request/Gift: " + (item.isLookingFor() ? "No" : "Yes"));

        TextView iCon = (TextView) findViewById(R.id.item_contact);
        iCon.setText("Contact info: " + owner.getContact());
        Button item_resolve = (Button)findViewById(R.id.item_resolve);
        if ((sameuser) && !(item.isResolve()))    //was ist mein benutzername?
        {
            item_resolve.setVisibility(View.VISIBLE);
        }
        else
        {
            item_resolve.setVisibility(View.INVISIBLE);
        }

    }

    /**
     * Called when the "Back" button is pressed. Goes back to the home screen.
     * @param view  the content view
     */
    public void goBack(View view)
    {
        Intent resultIntent = new Intent();
        resultIntent.putExtra("search", false);
        resultIntent.putExtra("changed", updated);
        setResult(Activity.RESULT_OK, resultIntent);
        finish();
    }

    /**
     * Called when the "Find similar items" button is pressed.
     * Sends back data to the item list for filtering
     */
    public void tryMatch(View view)
    {
        Intent resultIntent = new Intent();
        resultIntent.putExtra("search", true);
        resultIntent.putExtra("changed", true);
        resultIntent.putExtra("itemName", item.toString());
        resultIntent.putExtra("itemCat", item.getCategory());
        resultIntent.putExtra("itemType", item.isState());    //lost or found

        resultIntent.putExtra("itemLat", item.getLat());
        resultIntent.putExtra("itemLong", item.getLon());

        setResult(Activity.RESULT_OK, resultIntent);
        finish();
    }

    /**
     * Called when the "Resolve" button is pressed. Resolves the item.
     */
    public void resolve(View view)
    {
        if(view.getId() == R.id.item_resolve)
        {
            for(Item e: owner.getItems())
            {
                if(e.equals(item))
                {
                    e.setResolve();
                    iStatus.setText(("Closed"));
                    updated = true;

                    Button item_resolve = (Button)findViewById(R.id.item_resolve);

                    item_resolve.setVisibility(View.INVISIBLE);
                }
            }
            DataBase db = new DataBase(this);
            db.open();
            db.update(owner);
            db.close();
        }
    }
}
